<?php
namespace Home\Model;
use Think\Model;
//goods表操作类
class BuycarModel extends Model{     
    //写进购物车方法
    public function addbc(){
     //获取用户购买信息
        $list= I('get.');
        $uid = session('user')['id'];
        $list['uid'] = $uid; 
        //查看用户购物车中是否有相同商品
        $map['uid'] = ['eq',$list['uid']];
        $map['gid'] = ['eq',$list['gid']];
        $result = $this->where($map)->find();
        if($result){
            //取得当前数量
            $result['number'] += $list['number'];
            //积分
            $result['integral'] +=$list['integral'] * $list['number'];
            //总价格
            $result['total'] += $list['total'];
            //执行修改操作
            $res = $this->where($map)->save($result);
            //判断是否添加成功
            if($res > 0){
                return '购物成功';
            }
        }else{
            $list['integral'] = $list['integral'] * $list['number'];
            //写进购物车表
            
            $res = $this->add($list);
            //判断是否添加成功
            if($res > 0){
                return '购物成功';
            }else{
                return '购物失败，请重新购买';
            }
        }
        
    }
    //查询数据方法
    public function selc(){
        //查找用户添加进购物车的商品
        
        $uid = session('user')['id'];
        //设置查找条件
        $map['uid'] = ['eq',$uid];
       
        $list = $this->where($map)->select();
        // dump($list);二维数组
        //查询购物车数据条数
       // dump($list);
        //查询商品状态是否为1
        $map['status'] = ['eq',1];
        $slist = $this->where($map)->select();
        $num = count($slist);
        // dump($list[i]['total']);
        for($i = 0;$i<$num;$i++){
            // dump(i);
            $total +=$slist[$i]['total'];
            // dump($list[i]['total']);
        }
        return [
           
            'list' => $list,
            // 总价格
            'total'=>$total
        ];
    }
    //删除
    public function del_buycar(){
        $id = I('get.id');
        $res = $this->delete($id);
        return $res;
    }
    //做减修改
    public function up_buycar(){
        //获取用户要修改的数据
        $id = I('get.id');
        $number = I('get.number');
        $price = I('get.price');
        $integral = I('get.integral');
        $number -=1;
        //如果该商品数量为0，那就删除该商品
        if($number == 0){
            $res = $this->delete($id);
            return $res;
        }
        //设置修改条件
        $map['id'] = ['eq',$id];
        //设置要修改的数据
        $sb['number'] = $number;
        $sb['total'] = $number*$price;
        $sb['integral'] = $integral - 50;
        //做出修改时将状态改为1
        $sb['status'] = 1;
        // return  $sb['total'];
        // $res = $this->where($map)->save();
        $res =  $this-> where($map)->data($sb)->save();
        return $res;  
    }
    //做加修改
    public function up_buycart(){
        //获取用户要修改的数据
        $id = I('get.id');
        $number = I('get.number');
        $price = I('get.price');
        $integral = I('get.integral');
        // return $integral;
        $number += 1;
         //设置修改条件
        $map['id'] = ['eq',$id];
        //设置要修改的数据
        $sb['number'] = $number;
        $sb['integral'] = $integral + 50;
         //做出修改时将状态改为1
        $sb['status'] = 1;
        $sb['total'] = $number*$price;
        // $res = $this->where($map)->save();
        $res =  $this-> where($map)->data($sb)->save();
        return $res;  
    }
    //清空购物车
    public function clear_bcd(){
        $id = I('get.uid');
        //设置清空条件
        $map['uid'] = ['eq',$id];
        //执行
        $res = $this->where($map)->delete();
        return $res;

    }
    //查找商品总价格
     public function total_sec(){
        //查找用户添加进购物车的商品
        
        $uid = session('user')['id'];
        //设置查找条件
        $map['uid'] = ['eq',$uid];
       
        $list = $this->where($map)->select();
        // dump($list);二维数组
        //查询购物车数据条数
       // dump($list);
        $num = count($list);
        // dump($list[i]['total']);
        for($i = 0;$i<$num;$i++){
            // dump(i);
            $total +=$list[$i]['total'];
            // dump($list[i]['total']);
        }
        return $total; 
    }
    //改变用户状态
    public function status_sec(){
        $id = I('get.id');
        //查询当前状态
        $map['id'] = ['eq',$id];
        $res = $this->where($map)->field('status')->find();
        // return $res;
        if($res['status'] == 1){
          //修改为0
            $data = ['status'=>0];
            $res = $this->where($map)->save($data);  
            //查询当前商品的总价
            $total = $this->where($map)->find();
            $total = -$total['total'];
        }else{
           $data = ['status'=>1];
           $res = $this->where($map)->save($data);  
           //查询当前商品的总价
            $total = $this->where($map)->find();
            $total = $total['total']; 
        }
        return $total;
    }
    //确认订单方法查询
    public function order_selc(){
        $uid = session('user')['id'];
        //设置查询条件
        $map['uid'] = ['eq',$uid];
        $map['status'] = ['eq',1];
        $list = $this->where($map)->select();
        $num = count($list);
        // dump($list[i]['total']);
        for($i = 0;$i<$num;$i++){
            // dump(i);
            $total +=$list[$i]['total'];
            $integral +=$list[$i]['integral'];
            // dump($list[i]['total']);
        }
        return [
            // 用户列表
            'list' => $list,
            // 总价格
            'total'=>$total,
            // 总积分
            'integral'=>$integral
        ];
    }

//====20170124===================================================
}